#include "myTask.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "event_groups.h"
#include "queue.h"

#include "armaction.h"
#include "armeventctrl.h"

#include "armmotor.h"
#include "stepper.h"
#include "stepperctrl.h"
#include "usarttransmitter.h"
#include "communication.h"
#include "switch.h"
#include "manage.h"

#include "RelayMotor.h"
#include "push_rod.h"

#include "bsp_timer.h"
#include "bsp_usart.h"
#include "bsp_gpio.h"
#include "bsp_adc.h"
#include "bsp_can1.h"
#include "string.h"
#include "iap.h"
#include "bsp_can2.h"
#include "K100_CD.h"

extern QueueHandle_t ServerCmd_Queue;	//串口1接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can1_Tx_Queue;		//CAN信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can1_Rx_Queue;
extern QueueHandle_t Can2_Tx_Queue;		//CAN信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can2_Rx_Queue;
extern QueueHandle_t Usart3_Rx_Queue;	//串口3接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Usart6_Rx_Queue;	//串口6信息队列句柄，用于中断和任务通讯



//LightLimit_task
void LightLimit_task(void *pvParameters)
{
	uint8_t cnt = 0;
	while(1)
	{
		ArmMotorLimitCtrl();
		if(cnt++>10){
			cnt = 0;LED_B_TOGGLE();
		}
		
		if(!Get_ArmReset())
			reset2isp();
        
        vTaskDelay(10);
	}
}

void step_motor_task(void *pvParameters)
{
	uint8_t cnt = 0;
	while(1)
	{
		ArmMotorCtrl();
		PUSH_ROD_CTRL();
		if(cnt++>10){
			cnt = 0;LED_R_TOGGLE();
		}
        vTaskDelay(10);
	}
}

//Armevent_task
void Armevent_task(void *pvParameters)
{
	uint8_t cnt = 0;
	while(1)
	{
		ArmEventHandle();
		if(cnt++>10){
			cnt = 0;LED_G_TOGGLE();
		}
        vTaskDelay(10);
	}
}


CanRxMsg  Main_Board_MSG;
void comunication_task(void *pvParameters)
{
    CanRxMsg s_rx_message2;
	while(1)
	{
        if(xQueueReceive(Can1_Rx_Queue,&Main_Board_MSG,5)==pdTRUE){
            Can1Process(Main_Board_MSG.StdId,Main_Board_MSG.Data);
        }
        if(xQueueReceive(Can2_Rx_Queue,&s_rx_message2,1)==pdTRUE){
            K100TX_fun.K100CD_Data_Parse(s_rx_message2.StdId,s_rx_message2.Data);
        }
	   vTaskDelay(100);		
    }
}
//sensor_task
void sensor_task(void *pvParameters)
{
	while(1)
	{
        Send_Can1_Data_By_Queue();
        K100TX_fun.K100_CD_Init();
        Send_Can2_Data_By_Queue();
		vTaskDelay(10);
	}
}
/*
*返回状态数据给上级
*/
void Driver_Data_Request(void)
{
    CanTxMsg s_tx_message;
    s_tx_message.IDE = CAN_ID_STD;    //标准帧
    s_tx_message.RTR = CAN_RTR_DATA;  //数据帧
    s_tx_message.DLC = 8;          //帧长度
    s_tx_message.StdId = MAIN_BOARD_SENSOR;//帧ID为传入参数的CAN_ID
    
    s_tx_message.Data[0] = GetHangarState(CLIP);
    s_tx_message.Data[1] = GetHangarState(CHARGE);
    s_tx_message.Data[2] = GetHangarState(DOOR);
    s_tx_message.Data[3] = GetHangarState(LOCK);
    s_tx_message.Data[4] = k100cd_info_data.buf[4];   //充电电流
    s_tx_message.Data[5] = k100cd_info_data.buf[0];   //电池电量
//	
    
	xQueueSend(Can1_Tx_Queue,&s_tx_message,5);
}


extern uint16_t K100CD_Frame;
uint16_t debug = 0x00;
void offline_task(void *pvParameters)
{
	uint16_t Time_cnt = 0;
	while(1)
	{
		if(Time_cnt >= 4)
		{
			debug = K100CD_Frame;
			if( K100CD_Frame <1)  
				memset(k100cd_info_data.buf,0,6);
			Time_cnt = 0;
			K100CD_Frame = 0;
		}
		else
			Time_cnt++;
        SwitchTest();
        Driver_Data_Request();
	    Hangar_State_Manage();
		Stepper_Request();
		vTaskDelay(500);	
	}
}




